package com.example.layui_project.comm.config;

import com.example.layui_project.entity.SysPermission;
import com.example.layui_project.entity.SysRole;
import com.example.layui_project.entity.SysUser;
import com.example.layui_project.service.UserService;
import com.example.layui_project.utils.BestUitls;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;

@Component
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    /**
     * 用来为当前登录成功的用户授予权限和角色
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
         String username = (String) principalCollection.getPrimaryPrincipal();
        SysUser allRoleAndPermissionByUser = userService.getAllRoleAndPermissionByUser(username);
        HashSet<String> roles = new HashSet<>();
        /*roles.add("超级管理员");*/
        HashSet<String> permissions = new HashSet<>();
        if(BestUitls.isNotBlank(allRoleAndPermissionByUser)){
            // 设置role的目的是为了左侧菜单栏目可以通过该角色进行验证访问
            List<SysRole> rolesByUser = allRoleAndPermissionByUser.getRoles();
            rolesByUser.forEach((r)->{
                roles.add(r.getRolename());
                List<SysPermission> sysPermissionList = r.getSysPermissionList();
                sysPermissionList.forEach((s)->{
                    if(s.getType().equals("菜单")){
                        // 通过每次请求的href来做按钮级别的权限控制
                        permissions.add(s.getHref());
                    }
                });
            });
        }
        System.out.println(allRoleAndPermissionByUser);
        /**
         * 这个地方应该根据当前用户去库里面查询出所具有的角色我这里方便演示，写死一个角色
         */
        System.out.println(username+"执行了授权 doGetAuthorizationInfo");
        // 根据username去查询出该用户的所有角色和权限

        SimpleAuthorizationInfo simpInfo = new SimpleAuthorizationInfo();
        simpInfo.setRoles(roles);
        simpInfo.setStringPermissions(permissions);
        System.out.println("设置角色是："+roles);
        System.out.println("设置权限是："+permissions);
        return simpInfo;
    }

    /**
     * 验证我们的账号密码是否正确达到登录的标准
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        if(username==null){
            return null;
        }
        SysUser user = userService.isExist(username);
        if(user==null){ // 没有该用户
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, user.getPassword(), "");
        return simpleAuthenticationInfo;
    }
}
